package a66weding.com.jiehuntong.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuAdapter;
import org.xutils.x;
import java.util.ArrayList;
import java.util.List;
import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.beans.MeCollectBean;

/**
 * Created by YOLANDA on 2016/7/22.
 */
public class MenuAdapter extends SwipeMenuAdapter<MenuAdapter.DefaultViewHolder> {
    private List<MeCollectBean.DataBean> data = new ArrayList<>();
    public static OnItemClickListener mOnItemClickListener;
    public MenuAdapter(List<MeCollectBean.DataBean> data) {
        if (data != null) {
            this.data = data;
        }
    }
    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.item_me_indent_collect, parent, false);
    }

    @Override
    public DefaultViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new DefaultViewHolder(realContentView);
    }

    public List<MeCollectBean.DataBean> getData() {
        return data;
    }

    public MeCollectBean.DataBean getItemData(int position){
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    public void append(List<MeCollectBean.DataBean> data) {
        if (this.data != null) {
            this.data.addAll(data);
        } else {
            this.data = data;
        }
        notifyDataSetChanged();
    }

    public void clear() {
        if (data != null) {
            this.data.clear();
        }
    }
    public void remove(int position) {
        data.remove(position);
        notifyItemRemoved(position);
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }
    @Override
    public void onBindViewHolder(DefaultViewHolder holder, int position) {
        holder.setData(data.get(position));
    }


    @Override
    public int getItemCount() {
         return data == null ? 0 : data.size();
    }
    static class DefaultViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView mImageView;
        private TextView mCodeTv;
        private TextView mClassifyTv;
        private TextView mMoney;
        private Button mDeleteBtn;
        private LinearLayout mOnclickLay;
        private LinearLayout mSwipeLay;
        public DefaultViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            mImageView = (ImageView) itemView.findViewById(R.id.img_indent);
            mCodeTv = (TextView) itemView.findViewById(R.id.tv_indent_code);
            mClassifyTv = (TextView) itemView.findViewById(R.id.tv_me_classify);
            mMoney = (TextView) itemView.findViewById(R.id.tv_money);
            mDeleteBtn = (Button) itemView.findViewById(R.id.btn_delete);
            mSwipeLay = (LinearLayout) itemView.findViewById(R.id.sw_me_delete);
            mOnclickLay = (LinearLayout) itemView.findViewById(R.id.ll_me_indent_collect);
        }

        public void setOnItemClickListener(MenuAdapter.OnItemClickListener onItemClickListener) {
            mOnItemClickListener = onItemClickListener;
        }

        public void setData(MeCollectBean.DataBean data) {
            this.mCodeTv.setText(data.getNvc_Name());
            this.mClassifyTv.setText(data.getProductCategory().getNvc_Name());
            this.mMoney.setText("¥ " + data.getDm_ShopPrice());
            x.image().bind(this.mImageView, data.getNvc_PicMain());
        }
        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(getAdapterPosition());
            }
        }
    }
    public interface OnItemClickListener{
    void    onItemClick(int position);
    }

}
